<?php

namespace app\api\controller;

use app\common\controller\Api;
use think\Db;
use fast\Random;

/**
 * 商品接口
 */
class Goods extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    /**
     * @ApiTitle    (发布商品展示游戏)
     * @ApiSummary  (发布商品展示游戏)
     * @ApiMethod   (POST)
     * @ApiRoute    (api/Goods/games)
     * @ApiHeaders	(name=token, type=string, required=true, description="请求的Token")
     * @ApiParams   (name="type", type="varchar", required=false, description="network:端游 mobile:手游")
     * @ApiParams   (name="first", type="varchar", required=false, description="首字母")
     * @ApiParams   (name="shop_type", type="varchar", required=false, description="sell 我要卖   buy 我要买")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturn   ({
    "code": 1,
    "msg": "ok",
    "time": "1627553755",
    "data": {
    {
            "id": 1,
            "game": "王者荣耀",
            "image": "http://qbl.780g.com/uploads/20211013/4d2542db5824e0ae2006dbe090240be6.jpg",
            "first": "首字母",
            "type": "类型",
            "is_hot": 0,
            "info": {
                "id": 1,
                "name": 名称,
            },

        },

     * })
     */
    public function games()
    {
        $token = $this->request->header('token');
        $uid = Db::name('personal')->where('token',$token)->value('id');
        $im_type = $this->request->post('shop_type');
        if ($im_type == 'sell') {
            $this->is_login($token);
            $user = Db::name('personal')->where('id',$uid)->find();
            if ($user['is_real'] != 1) {
                $this->error('未实名',[],101);
            }
        }
        $type = $this->request->post('type');
        $first = $this->request->post('first');
        if (!empty($first)) {
            $map['first'] = $first;
        }
        if (!empty($type)) {
            $map['type'] = $type;
        }
        $map['status'] = 'normal';
        $res = Db::name('game')->where($map)->select();
        foreach ($res as $k=>$v) {
            $res[$k]['image'] = config('api_image_url') . $v['image'];
            $res[$k]['info'] = Db::name('game_info')->field('id,pid,name')->where('game_id',$v['id'])->where('gt_id',1)->select();
        }
        $this->success('ok',$res);
    }



    /**
     * @ApiTitle    (选择商品游戏区服)
     * @ApiSummary  (选择商品游戏区服)
     * @ApiMethod   (POST)
     * @ApiRoute    (api/Goods/constituency)
     * @ApiHeaders	(name=token, type=string, required=true, description="请求的Token")
     * @ApiParams   (name="id", type="varchar", required=false, description="游戏id")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturn   ({
    "code": 1,
    "msg": "ok",
    "time": "1627553755",
    "data": {
    'id':1

     * })
     */
    public function constituency()
    {
        $id = $this->request->post('id');
        $res = Db::name('game')->field('id,game,type')->where('id',$id)->find();
        if ($res['type'] == 'mobile') {
            $res['plat'] = Db::name('game_info')->field('id,pid,name')->where('game_id',$id)->where('gt_id',1)->where('status','normal')->select();
            foreach ($res['plat'] as $k=>$v) {
                $res['plat'][$k]['type'] = Db::name('game_info')->field('id,pid,name')->where('game_id',$id)->where('gt_id',5)->where('status','normal')->select();
                $res['plat'][$k]['operator'] = Db::name('game_info')->field('id,pid,name')->where('pid',$v['id'])->where('gt_id',2)->where('status','normal')->select();
                foreach ($res['plat'][$k]['operator'] as $kk=>$vv) {
                    $res['plat'][$k]['operator'][$kk]['area'] = Db::name('game_info')->field('id,pid,name')->where('pid',$vv['id'])->where('gt_id',3)->where('status','normal')->select();
                    foreach ($res['plat'][$k]['operator'][$kk]['area'] as $key=>$val) {
                        $res['plat'][$k]['operator'][$kk]['area'][$key]['server'] = Db::name('game_info')->field('id,pid,name')->where('pid',$val['id'])->where('gt_id',4)->where('status','normal')->select();
                    }
                }
            }
        } elseif ($res['type'] == 'network') {
            $res['type'] = Db::name('game_info')->field('id,pid,name')->where('game_id',$id)->where('gt_id',5)->where('status','normal')->select();
            $res['area'] = Db::name('game_info')->field('id,pid,name')->where('game_id',$id)->where('gt_id',3)->where('status','normal')->select();
            foreach ($res['area'] as $k=>$v) {
                $res['area'][$k]['server'] = Db::name('game_info')->field('id,pid,name')->where('pid',$v['id'])->where('gt_id',4)->where('status','normal')->select();
            }
        }

        $this->success('ok',$res);
    }




    /**
     * @ApiTitle    (商品列表)
     * @ApiSummary  (商品列表)
     * @ApiMethod   (POST)
     * @ApiRoute    (api/Goods/goodsList)
     * @ApiHeaders	(name=token, type=string, required=true, description="请求的Token")
     * @ApiParams   (name="id", type="varchar", required=true, description="游戏id")
     * @ApiParams   (name="type", type="varchar", required=false, description="游戏类型id")
     * @ApiParams   (name="plat", type="varchar", required=false, description="平台id")
     * @ApiParams   (name="area", type="varchar", required=false, description="所在区id")
     * @ApiParams   (name="keyword", type="varchar", required=false, description="关键词")
     * @ApiParams   (name="zy", type="varchar", required=false, description="阵营")
     * @ApiParams   (name="service", type="varchar", required=false, description="账号安全服务 1终身包赔2不服务3心意购")
     * @ApiParams   (name="sort", type="varchar", required=false, description="排序字段 普通排序:createtime 价格排序:price 最新排序:createtime 金币比例排序:coin_ratio")
     * @ApiParams   (name="order", type="varchar", required=false, description="排序方式 普通排序:asc 价格排序:desc 最新排序:desc 金币比例排序:desc")
     * @ApiParams   (name="method", type="varchar", required=false, description="1 平台代发 2卖家发货")
     * @ApiParams   (name="min_price", type="varchar", required=false, description="最小价格")
     * @ApiParams   (name="max_price", type="varchar", required=false, description="最高价格")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturn   ({
    "code": 1,
    "msg": "ok",
    "time": "1627553755",
    "data": {
            "id": 1,
            "goods_sn": "商品单号",
            "uid": 1,
            "buy_type": 1,
            "gameid": 1,
            "gamename": "王者荣耀",
            "goods_type": 1,
            "title": "王者荣耀208英雄300皮",
            "price": "1088.00",
            "havetime": 1634011200,
            "stock": null,
            "area": null,
            "status": 0,
            "createtime": 1634022470

     * })
     */
    public function goodsList()
    {
        $id = $this->request->post('id');
        $type = $this->request->post('type');
        $plat = $this->request->post('plat');
        $area = $this->request->post('area');
        $operator = $this->request->post('operator');
        $server = $this->request->post('server');
        $sort = $this->request->post('sort');
        $order = $this->request->post('order');
        $keyword = $this->request->post('keyword');
        $zy = $this->request->post('zy');
        $service = $this->request->post('service');
        $cross = $this->request->post('cross');
        $method = $this->request->post('method');
        $minPrice = $this->request->post('min_price');
        $maxPrice = $this->request->post('max_price');
        $sort = $sort ? $sort : 'createtime';
        $order = $order ? $order : 'asc';
        if (empty($type)) {
            $map['goods_type'] = Db::name('game_info')->field('id,game')->where('game_id',$id)->where('gt_id',5)->value('id');
        } else {
            $map['goods_type'] = $type;
        }
        if (!empty($plat)) {
            $map['plat_id'] = $plat;
        }
        if (!empty($area)) {
            $map['area_id'] = $area;
        }
        if (!empty($operator)) {
            $map['operator_id'] = $operator;
        }
        if (!empty($server)) {
            $map['server_id'] = $server;
        }
        if (!empty($keyword)) {
            $map['title'] = array('like','%'.$keyword.'%');
        }
        if (!empty($zy)) {
            $map['sku'] = array('like','%'.$zy.'%');
        }
        if (!empty($service)) {
            $map['is_safe'] = $service;
        }
        if (!empty($cross)) {
            $map['cross_id'] = $cross;
            unset($map['area_id']);
            unset($map['server_id']);
        }
        if (!empty($method)) {
            $map['method'] = $method;
        }
        if (!empty($minPrice)) {
            if (!empty($maxPrice)) {
                $map['price'] = ['between',[$minPrice,$maxPrice]];
            } else {
                $map['price'] = array('egt',$minPrice);
            }
        }
        $map['stock'] = ['gt',0];
        $map['gameid'] = $id;
        $map['status'] = 1;
        $res['vip'] = Db::name('receiving')
            ->field('id,class_type,title,server,cross,type_name,num,price,max,min,specs')
            ->where('num','gt',0)
            ->where('game_id',$id)
            ->where('status',1)
            ->where('cross_id',$cross)
            ->where('class_type',1)
            ->order('createtime desc')
            ->limit(3)
            ->select();
        foreach ($res['vip'] as $k=>$v) {
            if ($v['price'] >0 || $v['num']>0) {
                $v['fc'] =  round($v['price'],4);
                $v['zc'] =  round(1/$v['price'],2);
            }
        }
        $res['list'] = Db::name('goods')
            ->field('id,goods_class_type,title,server,cross,type_name,stock,price,image,is_safe,is_pei,sku,info,method,coin_ratio,coin')
            ->where($map)
            ->order($sort,$order)
            ->paginate(30,false,['query'=>request()->param()])
            ->each(function ($item,$key){
                if ($item['coin'] > 0) {
                    $item['fc'] = round($item['price']/$item['coin'],4);
                }
                if ($item['type_name'] != '') {
                    $specs = Db::name('attr')->where('type',$item['type_name'])->value('value');
                    $item['specs'] = $specs ? $specs : '万金';
                }
                $item['sku'] = htmlspecialchars_decode($item['sku']);
                $item['sku'] = json_decode($item['sku'],true);
                if ($item['goods_class_type'] == 1) {
                    $item['info'] = htmlspecialchars_decode($item['info']);
                    $item['info'] = json_decode($item['info'],true);
                } else {
                    $item['info'] = [];
                }

                $item['image'] = config('api_image_url') . $item['image'];
                if ($item['goods_class_type'] != 1) {
                    unset($item['image']);
                }
                return $item;
            });
        $this->success('ok',$res);
    }

    /**
     * @ApiTitle    (商品列表上搜索展示)
     * @ApiSummary  (商品列表上搜索展示)
     * @ApiMethod   (POST)
     * @ApiRoute    (api/Goods/first)
     * @ApiParams   (name="id", type="varchar", required=true, description="游戏id")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturn   ({
    "code": 1,
    "msg": "ok",
    "time": "1627553755",
    "data": {
    "type": 类型,
    "plat": 平台,
    "area": 区,


     * })
     */
    public function first()
    {
        $id = $this->request->post('id');
        if ($id == 2) {
            $res['cross'] = Db::name('gamecross')->field('id,name')->select();
        }
        $res['type'] = Db::name('game_info')->field('id,name')->where('status','normal')->where('game_id',$id)->where('gt_id',5)->select();
        $game = Db::name('game')->field('id,type,game,image')->where('id',$id)->find();
        $game['image'] = config('api_image_url') . $game['image'];
        if ($game['type'] == 'mobile') {
            $game['xx'] = '游戏平台';
            $game['plat'] = Db::name('game_info')->field('id,pid,name')->where('game_id',$id)->where('gt_id',1)->where('status','normal')->select();
            foreach ($game['plat'] as $k=>$v) {
                $game['plat'][$k]['xx'] = '运营商';
                $game['plat'][$k]['operator'] = Db::name('game_info')->field('id,pid,name')->where('pid',$v['id'])->where('gt_id',2)->where('status','normal')->select();
                foreach ($game['plat'][$k]['operator'] as $kk=>$vv) {
                    $game['plat'][$k]['operator'][$kk]['xx'] = '所在区';
                    $game['plat'][$k]['operator'][$kk]['area'] = Db::name('game_info')->field('id,pid,name')->where('pid',$vv['id'])->where('gt_id',3)->where('status','normal')->select();
                    foreach ($game['plat'][$k]['operator'][$kk]['area'] as $key=>$val) {
                        $game['plat'][$k]['operator'][$kk]['area'][$key]['xx'] = '所在服';
                        $game['plat'][$k]['operator'][$kk]['area'][$key]['server'] = Db::name('game_info')->field('id,pid,name')->where('pid',$val['id'])->where('gt_id',4)->where('status','normal')->select();
                    }
                }
            }
        } elseif ($game['type'] == 'network') {
            $game['area'] = Db::name('game_info')->field('id,pid,name')->where('game_id',$id)->where('gt_id',3)->where('status','normal')->select();
            $game['xx'] = '所在区';
            foreach ($game['area'] as $k=>$v) {
                $game['area'][$k]['xx'] = '所在服';
                $game['area'][$k]['server'] = Db::name('game_info')->field('id,pid,name')->where('pid',$v['id'])->where('gt_id',4)->where('status','normal')->select();
            }
        }
        $res['game'] = $game;
        $this->success('ok',$res);
    }


    /**
     * @ApiTitle    (商品详情)
     * @ApiSummary  (商品详情)
     * @ApiMethod   (POST)
     * @ApiRoute    (api/Goods/goodsInfo)
     * @ApiParams   (name="id", type="varchar", required=false, description="商品id")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturn   ({
            "code": 1,
            "msg": "ok",
            "time": "1627553755",
            "data": {
                "id": 商品id,
                "goods_sn": 商品编号,
                "uid": 卖家id,
                "buy_type": 卖的方式,
                "gameid": 游戏id,
                "iamge": 图片,
                "images": 详情上传图片,
                "i_image": null,
                "gamename": "王者荣耀",
                "goods_type": 类型,
                "type_name": 类型名称,
                "title": 标题,
                "price": 价格,
                "stock": 库存,
                "area": 区服,
                "createtime": 日期
                "buyInfo": {
                    "id": 买家id,
                    "okorder": 成交订单数量,
                    "noorder": 未成交订单数量,
                    "ok": 成交率
                }
            }
     * })
     */
    public function goodsInfo()
    {
        $id = $this->request->post('id');
        $token = $this->request->header('token');
        $uid = Db::name('personal')->where('token',$token)->value('id');
        $data = Db::name('goods')
            ->field('id,goods_sn,uid,gamename,desc,image,images,kf_images,type_name,goods_type,title,price,plat,plat_id,operator,operator_id,area,area_id,server,server_id,cross,sku,info,stock,createtime,qq,mobile,is_warrant,is_appoint,jy_time,is_safe,is_pei,havetime,coin_ratio,coin,method')
            ->where('id',$id)
            ->find();
        if ($data['uid'] == $uid) {
            $data['buy_myself'] = 1;
        } elseif ($data['uid'] != $uid) {
            $data['buy_myself'] = 2;
        }
        $specs = Db::name('attr')->where('type',$data['type_name'])->value('value');
        $data['specs'] = $specs ? $specs : '万金';
        $data['image'] = config('api_image_url') . $data['image'];
        $data['images'] = str_replace('/uploads',config('api_image_url').'/uploads',$data['images']);
        $data['kf_images'] = str_replace('/uploads',config('api_image_url').'/uploads',$data['kf_images']);
        $col = Db::name('collection')->where('gid',$id)->where('uid',$uid)->where('status',0)->find();
        if ($col) {
            $data['col'] = 1;
        } else {
            $data['col'] = 2;
        }
        $bid = Db::name('personal')->field('id,avatar')->where('id',$data['uid'])->find();
        $bid['okorder'] = Db::name('order')->where('uid',$data['uid'])->where('status',5)->count();
        $bid['noorder'] = Db::name('order')->where('uid',$data['uid'])->where('status',0)->count();
        if ($bid['noorder'] + $bid['okorder'] != 0) {
            $bid['ok'] = round($bid['okorder']/($bid['okorder']+$bid['noorder'])*100,2) . '%';
        } else {
            $bid['ok'] = '0%';
        }
        $data['sku']=htmlspecialchars_decode($data['sku']);
        $data['info']=htmlspecialchars_decode($data['info']);
        $data['sku'] = json_decode($data['sku'],true);
        $data['info'] = json_decode($data['info'],true);
        $data['buyInfo'] = $bid;
        $this->success('ok',$data);
    }



    /**
     * @ApiTitle    (添加商品展示)
     * @ApiSummary  (添加商品展示)
     * @ApiMethod   (POST)
     * @ApiRoute    (api/Goods/addList)
     * @ApiHeaders	(name=token, type=string, required=true, description="请求的Token")
     * @ApiParams   (name="id", type="varchar", required=false, description="游戏id")
     * @ApiParams   (name="type", type="varchar", required=false, description="物品类型id")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturn   ({
    "code": 1,
    "msg": "ok",
    "time": "1627553755",
    "data": [
        {
            "id": 1,
            "name": "游戏属性",
            "value": [
            "属性值"
            ],
            "attr": "字段属性"11
        }]

     * })
     */
    public function addList()
    {
        $id = $this->request->post('id');
        $type = $this->request->post('type');
        $res = Db::name('game_attr')->field('id,name,value,attr,is_splice')->where(['game_id'=>$id,'type'=>$type])->select();
        $typeName = Db::name('game_info')->where('id',$type)->value('name');
        /*if ($typeName == '金币') {
            $res['specs'] = Db::name('attr')->where('type',$typeName)->value('value');
        } else {*/
        foreach ($res as $k=>$v) {
            $res[$k]['value'] = explode("\r\n", $v['value']);
        }
        //}
        $fee = Db::name('server')
            ->field('trade,method')
            ->where('game_id',$id)
            ->where('game_type',$type)
            ->where(['method'=>['in',['1','2']]])
            ->select();
        if (empty($fee)) {
            $list['fee1'] = 0.03;
            $list['fee2'] = 0.02;
        }
        foreach ($fee as $k=>$v) {
            if ($v['method'] == 1) {
                if ($v['trade'] != '') {
                    $list['fee1'] = $v['trade'];
                } else {
                    $list['fee1'] = 0.03;
                }
            } else {
                if ($v['trade'] != '') {
                    $list['fee2'] = $v['trade'];
                } else {
                    $list['fee2'] = 0.02;
                }
            }
        }
        $list['data'] = $res;
        if ($typeName != '') {
            $specs = Db::name('attr')->where('type',$typeName)->value('value');
            $list['specs'] = $specs?$specs:'万金';
        } else {
            $list['specs'] = '金币';
        }
        $this->success('ok',$list);
    }


    /**
     * @ApiTitle    (添加商品)
     * @ApiSummary  (添加商品)
     * @ApiMethod   (POST)
     * @ApiRoute    (api/Goods/goodsAdd)
     * @ApiHeaders	(name=token, type=string, required=true, description="请求的Token")
     * @ApiParams   (name="id", type="varchar", required=false, description="游戏id")
     * @ApiParams   (name="name", type="varchar", required=false, description="游戏名称")
     * @ApiParams   (name="desc", type="varchar", required=false, description="描述")
     * @ApiParams   (name="image", type="varchar", required=false, description="封面")
     * @ApiParams   (name="images", type="varchar", required=false, description="账号截图")
     * @ApiParams   (name="goods_type", type="varchar", required=false, description="商品类型id")
     * @ApiParams   (name="title", type="varchar", required=false, description="商品标题")
     * @ApiParams   (name="price", type="varchar", required=false, description="价格")
     * @ApiParams   (name="plat_id", type="varchar", required=false, description="游戏平台id")
     * @ApiParams   (name="operator_id", type="varchar", required=false, description="运营商id")
     * @ApiParams   (name="area_id", type="varchar", required=false, description="游戏区id")
     * @ApiParams   (name="server_id", type="varchar", required=false, description="服务器id")
     * @ApiParams   (name="sku", type="varchar", required=false, description="皮肤内容")
     * @ApiParams   (name="info", type="varchar", required=false, description="详情信息")
     * @ApiParams   (name="havetime", type="varchar", required=false, description="有效时间")
     * @ApiParams   (name="g_account", type="varchar", required=false, description="游戏账号")
     * @ApiParams   (name="g_password", type="varchar", required=false, description="游戏密码")
     * @ApiParams   (name="is_warrant", type="varchar", required=false, description="授权买家上号验号1开启2关闭")
     * @ApiParams   (name="is_appoint", type="varchar", required=false, description="是否指定买家购买1指定2不指定")
     * @ApiParams   (name="cipher", type="varchar", required=false, description="交易暗码")
     * @ApiParams   (name="is_safe", type="varchar", required=false, description="账号安全服务1终身包赔2不服务3心意购")
     * @ApiParams   (name="jy_time", type="varchar", required=false, description="交易时间 每天几点-几点")
     * @ApiParams   (name="is_pei", type="varchar", required=false, description="无货赔付 1:是 2:否")
     * @ApiParams   (name="mobile", type="varchar", required=false, description="手机号")
     * @ApiParams   (name="qq", type="varchar", required=false, description="QQ号")
     * @ApiParams   (name="stock", type="varchar", required=false, description="库存")
     * @ApiParams   (name="invite", type="varchar", required=false, description="推广码")
     * @ApiParams   (name="second_pwd", type="varchar", required=false, description="二级密码")
     * @ApiParams   (name="ware_pwd", type="varchar", required=false, description="仓库密码")
     * @ApiParams   (name="storage", type="varchar", required=false, description="物品存放处")
     * @ApiParams   (name="method", type="varchar", required=false, description="寄售方式 1平台代发 2卖家发货 3自动发货")
     * @ApiParams   (name="coin_type", type="varchar", required=false, description="金币交易 1 邮寄交易 2 拍卖交易 3 无")
     * @ApiParams   (name="ck", type="varchar", required=false, description="自抽号")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturn   ({
    "code": 1,
    "msg": "注册成功",
    "time": "1627553755",
    "data": {
    'id':1

     * })
     */
	public function goodsAdd()
	{
		$token = $this->request->header('token');
		$id = Db::name('personal')->where('token', $token)->value('id');
		$user = Db::name('personal')->where('id', $id)->find();
		if ($user['is_real'] != 1) {
			$this->error('未实名', [], 101);
		}
        $this->is_login($token);
		$shop_id = $this->request->post('shop_id');
		if (!empty($shop_id)) {
			Db::name('goods')->where('id', $shop_id)->update(['status' => 9]);
		}
		$goods_sn = $this->getRefundSn($id);
		$have = $this->request->post('havetime');
		$goods_type = $this->request->post('goods_type');
		$plat_id = $this->request->post('plat_id');
		$operator_id = $this->request->post('operator_id');
		$area_id = $this->request->post('area_id');
		$server_id = $this->request->post('server_id');
		$price = $this->request->post('price');
		$is_safe = $this->request->post('is_safe');
		$sku = $this->request->post('sku');
		$info = $this->request->post('info');
		$gameId = $this->request->post('id');
		$gameName = $this->request->post('name');
		$desc = $this->request->post('desc');
		$title = $this->request->post('title');
		$account = $this->request->post('g_account');
		$password = $this->request->post('g_password');
		$warrant = $this->request->post('is_warrant');
		$appoint = $this->request->post('is_appoint');
		$cipher = $this->request->post('cipher');
		$jy_time = $this->request->post('jy_time');
		$pei = $this->request->post('is_pei');
		$mobile = $this->request->post('mobile');
		$qq = $this->request->post('qq');
		$invite = $this->request->post('invite');
		$method = $this->request->post('method');
		$coin_type = $this->request->post('coin_type');
		$coin_ratio = $this->request->post('coin');
		$second_pwd = $this->request->post('second_pwd');
		$ware_pwd = $this->request->post('ware_pwd');
		$storage = $this->request->post('storage');
		$ck = $this->request->post('ck');
		$ck = htmlspecialchars_decode($ck);
		$have = $have ? intval($have) : 1;
		$coin_type = $coin_type ? $coin_type : 4;
		$coin_ratio = $coin_ratio ? $coin_ratio : 0;
		$desc = $desc ? $desc : '';
		$coin = $coin_ratio / $price;
		$typeName = Db::name('game_info')->where('id', $goods_type)->value('name');
		$plat = Db::name('game_info')->where('id', $plat_id)->value('name');
		$operator = Db::name('game_info')->where('id', $operator_id)->value('name');
		$area = Db::name('game_info')->where('id', $area_id)->value('name');
		$cross_id = Db::name('game_info')->where('id', $server_id)->value('cross');
		$server = Db::name('game_info')->where('id', $server_id)->value('name');
		$cross = Db::name('gamecross')->where('id', $cross_id)->value('name');
		/*if ($is_safe == 1) {
			$fee = Db::name('server')->where('id', 1)->find();
			$prices = $price * (1 + $fee['service']);
		} else {
			$prices = $price;
		}*/
		//add 根据不同游戏的手续费来判断
        $trade_fee = 0;
        if($is_safe == 1){
            $fee = DB::name("server")->where(['game_id'=>$gameId,'game_type'=>$goods_type,'safe'=>$is_safe,'method'=>$method])->find();
            if(!$fee){
                //没有则找默认设置
//            $fee = DB::name("server")->where(['game_id'=>$gameId,'game_type'=>$goods_type,'safe'=>$is_safe,'method'=>$method])->find();
                $fee = Db::name('server')->where('id', 1)->find();
            }
            $trade_fee = $fee['service'];
        }


		$prices = $price * (1 + $trade_fee);
		if ($typeName == '账号') {
			$type = 1;
		} elseif ($typeName == '金币') {
			$type = 2;
		} elseif ($typeName == '自抽号') {
			$type = 3;
		} elseif ($typeName == '装备') {
			$type = 4;
		} else {
			$type = 2;
		}
		if ($typeName == '账号') {
			$stock = 1;
			$GdCreateImage = new GdCreateImage();
			$gdImg = $GdCreateImage->index($sku, $info);
			$gdImg = json_decode($gdImg,true);
			$img = $this->request->post('images');
			$img = str_replace(config('api_image_url').'/uploads','/uploads',$img);
			$is_img = Db::name('game_info')->where('id',$goods_type)->value('is_skuimg');
			if ($is_img == 1) {
				$images = '/' . $gdImg['url'] . ',' . $img;
				$image = '/' . $gdImg['url'];
			} else {
				$images = $img;
				$img = explode(',',$img);
				$image = $img[0];
			}
			$status = 4;
		} elseif ($typeName == '自抽号') {
			$stock = $this->request->post('stock');
			$images = $this->request->post('images');
			$img = explode(',',$images);
			$image = $img[0];
			$status = 1;
		} else {
			$stock = $this->request->post('stock');
			$images = Db::name('game')->where('id', $gameId)->value('w_image');
			$image = Db::name('game')->where('id', $gameId)->value('image');
			$status = 1;
		}
		$res = [
			'uid'   =>  $id,
			'goods_class_type'  =>  $type,
			'goods_sn'  =>  $goods_sn,
			'gameid'    =>  $gameId,
			'gamename'    =>  $gameName,
			'desc'    =>  $desc,
			'image' =>  $image,
			'images'   =>  $images,
			'goods_type'    =>  $goods_type,
			'type_name' =>  $typeName,
			'title' =>  $title,
			'price'  =>  $prices,
			'prices'  =>  $price,
			'plat_id' =>  $plat_id,
			'plat'    =>  $plat,
			'operator_id'    =>  $operator_id,
			'operator'   =>  $operator,
			'area_id'    =>  $area_id,
			'area' =>  $area,
			'server_id'    =>  $server_id,
			'server'    =>  $server,
			'cross_id'    =>  $cross_id,
			'cross'    =>  $cross,
			'sku' =>  $sku,
			'info' =>  $info,
			'havetime' =>  time() + 86400 * $have,
			'g_account'   =>  $account,
			'g_password'   =>  $password,
			'is_warrant'  =>  $warrant,
			'is_appoint'   =>  $appoint,
			'cipher'   =>  $cipher,
			'is_safe'  =>  $is_safe,
			'jy_time'  =>  $jy_time,
			'is_pei'  =>  $pei,
			'stock'   =>  $stock,
			'mobile'  =>  $mobile,
			'qq'    =>  $qq,
			'status'    =>  $status,
			'method'    =>  $method,
			'invite'    =>  $invite,
			'coin_type'    =>  $coin_type,
			'coin'    =>  $coin_ratio,
			'coin_ratio'    =>  $coin,
			'createtime'    =>  time(),
			'second_pwd'  =>  $second_pwd,
			'ware_pwd'  =>  $ware_pwd,
			'storage'  =>  $storage
		];
        //添加分组，只有投诉咨询和一级客服可以分配
        $auth_group = DB::name("auth_group")->where(['name'=>['in',['投诉咨询','一级客服']]])->column('id');
        $auth_group_access = DB::name("auth_group_access")->where(['group_id'=>['in',$auth_group]])->column("uid");
        $admin_id = Db::name('admin')->where(['status'=>'normal','id'=>['in',$auth_group_access]])->orderRaw('Rand()')->value('id');
		$data = Db::name('goods')->insertGetId($res);
		if ($data) {
			if ($typeName == '自抽号' && $method != 2) {
				$ck = explode(',',$ck);
				$ck_res = [];
				$ck1 = [];
				foreach ($ck as $k=>$v) {
					$ckk = explode('&',$v);
					if (empty($ckk[1])) {
						$this->error('抱歉,商品库存格式填写不正确');
					}
					$ck_res[] = [
						'goods_id'  =>  $data,
						'cdkey' =>  $ckk[0],
						'pwd'   =>  $ckk[1],
						'status'    =>  0
					];
					$ck1[] = $ckk[0];
				}
				if (count($ck1) != count(array_unique($ck1))) {
					$this->error('抱歉,商品库存有重复账号');
				} else {
					Db::name('goodsinfo')->insertAll($ck_res);
				}
			}
			if ($status == 4) {
				$easy = new Goeasy();
				$msg = '您有新的待审核的商品了!';
				$easy->sendToAdmin($admin_id,$msg);
			}
			$this->success('恭喜,商品发布成功');
		} else {
			$this->error('抱歉,商品发布失败');
		}
	}

    /**
     * @ApiTitle    (商品收藏)
     * @ApiSummary  (商品收藏)
     * @ApiMethod   (POST)
     * @ApiRoute    (api/Goods/collection)
     * @ApiHeaders	(name=token, type=string, required=true, description="请求的Token")
     * @ApiParams   (name="gid", type="varchar", required=false, description="商品id")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturn   ({
    "code": 1,
    "msg": "收藏成功",
    "time": "1627553755",
    "data": {
    'id':1

     * })
     */
    public function collection()
    {
        $token = $this->request->header('token');
        $id = Db::name('personal')->where('token',$token)->value('id');
        $this->is_login($token);
        $gid = $this->request->post('gid');
        $is = Db::name('collection')->where('uid',$id)->where('gid',$gid)->where('status',0)->find();
        if (!empty($is)) {
            $no = Db::name('collection')->where('id',$is['id'])->update(['status'=>1]);
            if ($no) {
                $this->success('取消收藏成功');
            } else {
                $this->error('取消收藏失败');
            }
        }
        $res = [
            'uid'   =>  $id,
            'gid'   =>  $gid,
            'status'    =>  0,
            'createtime'    =>  time()
        ];
        $data = Db::name('collection')->insert($res);
        if ($data) {
            $this->success('收藏成功');
        } else {
            $this->error('收藏失败');
        }
    }



    /**
     * @ApiTitle    (视频认证)
     * @ApiSummary  (视频认证)
     * @ApiMethod   (POST)
     * @ApiRoute    (api/Goods/video)
     * @ApiHeaders	(name=token, type=string, required=true, description="请求的Token")
     * @ApiParams   (name="video", type="varchar", required=false, description="认证视频")
     * @ApiParams   (name="goods_id", type="varchar", required=false, description="商品id")
     * @ApiParams   (name="image", type="varchar", required=false, description="手持身份证照片")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturn   ({
    "code": 1,
    "msg": "提交审核成功,请等待",
    "time": "1627553755",

     * })
     */
    public function video()
    {
        $token = $this->request->header('token');
        $this->is_login($token);
        $video = $this->request->post('video');
        $image = $this->request->post('image');
        $gid = $this->request->post('goods_id');
        $res = [
            'video' =>  $video,
            'hold_card' =>  $image,
            'auth_status'   =>  1,
        ];
        $data = Db::name('goods')->where('id',$gid)->update($res);
        if ($data) {
            $this->success('提交审核成功,请等待');
        } else {
            $this->error('提交审核失败');
        }
    }

    /**
     * @ApiTitle    (视频认证页面)
     * @ApiSummary  (视频认证页面)
     * @ApiMethod   (POST)
     * @ApiRoute    (api/Goods/videoList)
     * @ApiHeaders	(name=token, type=string, required=true, description="请求的Token")
     * @ApiParams   (name="gid", type="varchar", required=false, description="商品id")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturn   ({
    "code": 1,
    "msg": "收藏成功",
    "time": "1627553755",
    "data": {
    'user':
    'game':

     * })
     */
    public function videoList()
    {
        $token = $this->request->header('token');
        $id = Db::name('personal')->where('token',$token)->value('id');
        $this->is_login($token);
        $gid = $this->request->post('gid');
        $res['user'] = Db::name('personal')->field('id,name,id_card,mobile')->where('id',$id)->find();
        $res['game'] = Db::name('goods')->field('id,createtime,gamename,type_name,g_account')->where('id',$gid)->find();
        $this->success('ok',$res);
    }


    /**
     * @ApiTitle    (修改价格)
     * @ApiSummary  (修改价格)
     * @ApiMethod   (POST)
     * @ApiRoute    (api/Goods/modify)
     * @ApiParams   (name="gid", type="varchar", required=false, description="商品id")
     * @ApiParams   (name="price", type="varchar", required=false, description="修改的价格")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturn   ({
    "code": 1,
    "msg": "修改成功",
    "time": "1627553755",
    "data": {
    'user':
    'game':

     * })
     */
    public function modify()
    {
        $id = $this->request->post('gid');
        $price = $this->request->post('price');
        $g = Db::name('goods')->where('id',$id)->find();
        $coin = $g['coin']/$price;
        $fee = Db::name('server')->where('id',1)->find();
        $prices = $price*(1+$fee['service']);
//        $order = Db::name('order')->where('goods_id',$id)->where('status','neq',5)->select();
//        if ($order) {
//            $this->error('此商品已下单,不可更改');
//        }
        if ($g['goods_class_type'] == 2) {
            if ($g['coin_type'] == 1) {
                $t = '【邮寄交易】';
            } elseif ($g['coin_type'] == 2) {
                $t = '【拍卖交易】';
            } else {
                $t = '【面对面交易】';
            }
            $title = $t.$price.'元='.$g['coin'].'万金';
            $data = Db::name('goods')->where('id',$id)->update(['price'=>$price,'prices'=>$price,'coin_ratio'=>$coin,'title'=>$title]);
        } else {
            if ($g['is_safe'] == 1) {
                $data = Db::name('goods')->where('id',$id)->update(['price'=>$prices,'prices'=>$price]);
            } else {
                $data = Db::name('goods')->where('id',$id)->update(['price'=>$price,'prices'=>$price]);
            }
        }
        if ($data) {
            $this->success('修改成功');
        } else {
            $this->error('修改失败');
        }
    }

}
